// pages/singleQuestion/singleQuestion.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    items: [
      {
        name: 0,
        checked: 'true',

      },
      {
        name: 1,
        checked: '',
      }
    ],
    currentAnswer: 0,
    currentRadioNum: 2
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight,
        })
      }
    })
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  radioChange: function (e) {
    console.log(e.detail.value)
    for (var i = 0; i < this.data.items.length; i++) {
      this.data.items[i].checked = ""
    }
    this.data.items[e.detail.value].checked = "true"
    this.setData({
      currentAnswer: e.detail.value
    })
  },
  addRadio: function () {
    if (this.data.currentRadioNum < 4) {
      this.data.currentRadioNum++;
      this.data.items.push({
        name: this.data.items.length,
        checked: ''
      })
      this.setData({
        items: this.data.items
      })
    }
  },
  reduceRadio: function () {
    if (this.data.currentRadioNum > 2) {
      this.data.currentRadioNum--;
      var _pop = this.data.items.pop()
      if (_pop.checked == "true") {
        this.setData({
          items: this.data.items,
          currentAnswer: -1
        })
      } else {
        this.setData({
          items: this.data.items,
          //   currentAnswer: -1
        })
      }

    }
  },
  formSubmit: function (e) {
    if (this.data.currentAnswer == -1) {
      wx.showModal({
        title: '提示',
        content: '请勾选正确答案',
      })
      return;
    }
    console.log(e.detail.value)
    var _question = e.detail.value.question;
    var _anser0 = e.detail.value.answer0;
    var _anser1 = e.detail.value.answer1;
    var _anser2, _anser3, _correct;
    if (!_question) {
      wx.showModal({
        title: '提示',
        content: '问题不能为空',
      })
      return;
    }
    if (!_anser0 || !_anser1) {
      wx.showModal({
        title: '提示',
        content: '答案不能为空',
      })
      return;
    }
    if (e.detail.value.answer2 == "" || e.detail.value.answer3 == "") {
      wx.showModal({
        title: '提示',
        content: '答案不能为空',
      })
      return;
    }
    if (e.detail.value.answer2 == undefined) {
      _anser2 = ""
    } else {
      _anser2 = 'C:'+e.detail.value.answer2;
    }
    if (e.detail.value.answer3 == undefined) {
      _anser3 = ""
    } else {
      _anser3 = 'D:'+e.detail.value.answer3;
    }
    var arr = [_anser0, _anser1, _anser2, _anser3];
    _correct = arr[e.detail.value.realAnswer];
    console.log(_anser2)
    wx.request({
      url: app.globalData.allUrl + "/api/one_question.php",
      data: {
        user_id: wx.getStorageSync('userid'),
        question: _question,
        an1:'A:'+ _anser0,
        an2:'B:' +_anser1,
        an3: _anser2,
        an4: _anser3,
        correct: _correct,
      },
      header: { 'content-type': "application/x-www-form-urlencoded" },
      method: 'POST',
      success: function (res) {
        console.log(res.data)
        wx.navigateTo({
          url: '../../pages/singleMoneyPakege/singleMoneyPakege?qid=' + res.data.qid,
        })
      }, fail: function () {
      }
    })
  },
  cancel: function () {
    wx.switchTab({
      url: '../../pages/index/index',
    })
  }
})